﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using umbraco.BusinessLogic.Actions;
using umbraco.cms.presentation.Trees;
using umbraco.interfaces;

namespace Our.Umbraco.uDbCompare
{
    public class uDbCompareTreeItem : BaseTree
    {
        public uDbCompareTreeItem(string application) : base(application)
        {
        }

        protected override void CreateRootNodeActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionNew.Instance);
            actions.Add(ContextMenuSeperator.Instance);
            actions.Add(ActionRefresh.Instance);
        }

        protected override void CreateAllowedActions(ref List<IAction> actions)
        {
            actions.Clear();
            actions.Add(ActionDelete.Instance);
        }

        protected override void CreateRootNode(ref XmlTreeNode rootNode)
        {
            rootNode.NodeType = TreeAlias;
            rootNode.Icon = "../../uDbCompare/images/s_database.png";
            rootNode.OpenIcon = "../../uDbCompare/images/s_database.png";
            rootNode.NodeID = "-1";

            rootNode.HasChildren = (uDbCompareCMSNode.GetAllNodes().Count > 0);
            rootNode.Action = "javascript:uDbCompareInfo();";
        }
        
        public override void Render(ref XmlTree tree)
        {
            if (this.id == -1)
            {
                foreach (var node in uDbCompareCMSNode.GetAllNodes())
                {
                    XmlTreeNode xNode = XmlTreeNode.Create(this);
                    xNode.NodeID = node.Id.ToString();
                    xNode.Text = node.Text;
                    xNode.Icon = "../../uDbCompare/images/s_database.png";
                    xNode.OpenIcon = "../../uDbCompare/images/s_database.png";
                    xNode.NodeType = TreeAlias;

                    xNode.Action = "javascript:uDbCompare(" + node.Id.ToString() + ");";
                    
                    tree.Add(xNode);
                }
            }
        }

        public override void RenderJS(ref StringBuilder Javascript)
        {
            Javascript.Append("function uDbCompareInfo() { parent.right.document.location.href = 'uDbCompare/info.aspx'; }");
            Javascript.Append("function uDbCompare(id) { parent.right.document.location.href = 'uDbCompare/index.aspx?id=' + id; }");
        }


    }
}
